var arrayOfBombs = [];
var assetsToLoader = ["SpriteSheet.json"];
var explosionTextures = [];
var explosions = [];
var count = 0;
var bLoaded = false;

function onAssetsLoaded()
{
	for (var i=0; i < 26; i++) 
	{
		var texture = PIXI.Texture.fromFrame("Explosion_Sequence_A " + (i+1) + ".png");
		explosionTextures.push(texture);
    };
bLoaded = true;

loadGame();
}


function CreateBomb(x, y, stage) {
    var bullet = new Bomb(x, y, stage);
    arrayOfBombs.push(bullet);
    return bullet;
}

function Bomb(x,y,stage)
{
    this.sprite = new PIXI.Sprite(getTexture("bomb"));
    this.position = new PIXI.Point(x, y);
    this.sprite.position= this.position;
	this.sprite.rotation = Math.random() * Math.PI;
	this.sprite.anchor.x = 0.5;
	this.sprite.anchor.y = 0.5;
	this.width = 32;
	this.height = 32;
	this.directionX = 1;
	this.directionY = 1;
	this.damage = 10000;
	this.owner = "BADGUY";
	this.range = 100;
	this.alive = true;
	
	this.blastRadious = 240;
	this.explodeTime = 150;
	this.exploding = false;

   // if(bLoaded == true)
	    this.explosion = new PIXI.MovieClip(explosionTextures);
	

	
	stage.addChild(this.sprite);

}

Bomb.prototype.hitTarget = function ()
	{
		
		if (this.sprite != null && this.alive == true ) {
		stage.removeChild(this.sprite);
		this.sprite = null;
		this.alive = false;
		}
	}
Bomb.prototype.update = function () {
    this.position.x += this.directionX;
    this.sprite.position.x += this.directionX;
	this.sprite.rotation += .4;
    this.position.y += this.directionY;
    this.sprite.position.y += this.directionY;
    this.range--;
    if (this.range <= 0 && this.alive == true && this.exploding == false) {
		
        this.alive = false;		
        this.sprite.stage.removeChild(this.sprite);
		
		
		this.explosion.position.x =this.position.x -(16 * 4.5);
		this.explosion.position.y =this.position.y -(16 * 4.5);
		//this.explosion.rotation = Math.random() * Math.PI;
		this.explosion.scale.x = this.explosion.scale.y = 0.75
		this.explosion.loop = false;
	    this.explosion.gotoAndPlay(1);	
		this.exploding = true;
		stage.addChild(this.explosion);
    }
	else if (this.exploding == true && this.explodeTime > 0 )
	{
		this.explodeTime--;
	}
	
	
}


function updateBombs() {
    for (var i = 0; i < arrayOfBombs.length; i++) {
        arrayOfBombs[i].update();
    }
}

function checkBombColissions()
{
	for (var i = 0; i < arrayOfBombs.length; i++) {
		if ( arrayOfBombs[i].exploding && arrayOfBombs[i].explodeTime >0 ) 
		{
			/*
				TO DO 
				Add logic here to get bounding box of the projectile!
				var myX = arrayOfProjectiles[i].sprite.position.x;
				var myY = arrayOfProjectiles[i].sprite.position.y;
				var myW = arrayOfProjectiles[i].width;
				var myH = arrayOfProjectiles[i].height;
			*/
			var myX = arrayOfBombs[i].sprite.position.x + 16;
			var myY = arrayOfBombs[i].sprite.position.y + 16;
			var myW = arrayOfBombs[i].width = arrayOfBombs[i].blastRadious;
			var myH = arrayOfBombs[i].height = arrayOfBombs[i].blastRadious;
			var myDmg =  arrayOfBombs[i].damage;
			
			if  (arrayOfBombs[i].owner == "BADGUY")
			{
				for (var j = 0; j < arrayOfBunnies.length; j++)
				{
					if (arrayOfBunnies[j].alive)
					{
						var bX = arrayOfBunnies[j].position.x;
						var bY = arrayOfBunnies[j].position.y;
						var bW = arrayOfBunnies[j].width;
						var bH = arrayOfBunnies[j].height;
						
						if (DOTHEYHITBOMBS(myX,myY,myW,myH,bX,bY,bW,bH))
						{
							/*
							stage.removeChild(arrayOfBunnies[j]);
							arrayOfBunnies[j].alive = false;
							*/
							arrayOfBunnies[j].hitBunny(myDmg);
							arrayOfBombs[i].hitTarget();
						}
					}
				}
			}
			else
			{
				for (var k = 0; k < arrayOfEnemies.length; k++)
				{
					if (arrayOfEnemies[k].alive)
					{
						var eX = arrayOfEnemies[k].position.x;
						var eY = arrayOfEnemies[k].position.y;
						var eW = arrayOfEnemies[k].width;
						var eH = arrayOfEnemies[k].height;
						
						if (DOTHEYHITBOMBS(myX,myY,myW,myH,eX,eY,eW,eH))
						{
							/*
							stage.removeChild(arrayOfBunnies[j]);
							arrayOfBunnies[j].alive = false;
							*/
							arrayOfEnemies[k].hitEnemy(myDmg);
							arrayOfBombs[i].hitTarget();
						}
					}
				
				}
			}
		
		}
    }
}

function DOTHEYHITBOMBS(x1,y1,w1,h1,x2,y2,w2,h2)
{
	//Top Left
	if( x2 <= x1+h1 && x2 >= x1 && y2 <= y1 +h1 && y2 >= y1 )
	{
		return true;
	}
	//Bottom Left
	if( x2 <= x1+h1 && x2 >= x1 && (y2 + h2) <= y1 +h1 && (y2 + h2) >= y1 )
	{
		return true;
	}
	//Top Right
	if( (x2 + w2) <= x1+h1 && (x2 + w2) >= x1 && y2 <= y1 + h1 && y2 >= y1 )
	{
		return true;
	}
	//Bottom Right 
	if( (x2 + w2) <= x1+h1 && (x2 + w2) >= x1 && (y2 + h2) <= y1 + h1 && (y2 + h2) >= y1 )
	{
		return true;
	}

	return false;
}


